import type { TablePaginationConfig, UploadProps } from "antd";
import { message, Upload } from "antd";
import { useTranslation } from "react-i18next";

//分页参数配置
export function createPaginationProps(
  total: number,
  defaultSize: number,
  changePage: (pageN: number, pageSize: number) => void
): TablePaginationConfig {
  const { t } = useTranslation();
  const pageSizeOptions = [5, 8, 12, 20];
  const defaultPageSize = pageSizeOptions.includes(defaultSize)
    ? defaultSize
    : pageSizeOptions[1];
  return {
    size: "small",
    total: total,
    showSizeChanger: true,
    showQuickJumper: true,
    pageSizeOptions: pageSizeOptions,
    defaultPageSize: defaultPageSize,
    showTotal: (total: number) => t("datatotal", { total: total }),
    onChange: (pageN: number, pageSize: number) => changePage(pageN, pageSize),
  };
}

//图片上传参数配置
export function createUploadProgress(url: string): UploadProps {
  return {
    name: "avatar",
    action: url,
    headers: { Authorization: `Bearer ${localStorage.getItem("my-token")!}` },
    listType: "picture-card",
    className: "avatar-uploader",
    beforeUpload: (file) => {
      const ext = file.name.split(".")[1];
      if (!["jpg", "png"].includes(ext)) {
        message.error("图片格式不正确");
        return Upload.LIST_IGNORE;
      }
    },
    progress: {
      strokeColor: {
        "0%": "#108ee9",
        "100%": "#87d068",
      },
      strokeWidth: 3,
      format: (percent) => percent && `${parseFloat(percent.toFixed(2))}%`,
    },
  };
}
